package com.wwwarehouse.taskcenter.fragment.job_point.step5;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.custom_widget.keyboard.VirtualKeyboardView;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.job_point.TaskPointUnitsetnumberAdapter;
import com.wwwarehouse.taskcenter.bean.job_point.AddJobPointStep5LookItemMesResponseBean;
import com.wwwarehouse.taskcenter.eventbus_event.JobPointSetnumberEvent;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/25 18:57
 * @描述 ${TODO}
 */

public class ChooseJobUnitSetnumberDetailFragment5 extends BaseFragment implements EasyPopupWindow.ChildClickListener {

    private View mView;
    private RecyclerView mTaskPointRecycleview;
    private StateLayout mSlLayout;
    private static final int pageSize = 10;
    private ArrayList<AddJobPointStep5LookItemMesResponseBean.ListBean> currentList;
    private EasyPopupWindow mEasyPopupWindow;
    private TaskPointUnitsetnumberAdapter adapter;
    private String currentUkid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_choose_object_detail_6, container, false);
        initView(mView);
        return mView;
    }


    @Override
    public void setTitle() {
    }

    @Override
    public void requestDatas() {

    }

    private void initView(View mView) {
        mTaskPointRecycleview = (RecyclerView) mView.findViewById(R.id.task_point_recycleview);
        mSlLayout = (StateLayout) mView.findViewById(R.id.sl_layout);

        mTaskPointRecycleview.setLayoutManager(new LinearLayoutManager(mActivity));

        int page = getArguments().getInt("page");
        ArrayList<AddJobPointStep5LookItemMesResponseBean.ListBean> list = getArguments().getParcelableArrayList("dataList");

        currentList = new ArrayList<>();
        for (int i = (page - 1) * pageSize; i < (list.size() > page * pageSize ? page * pageSize : list.size()); i++) {
            currentList.add(list.get(i));
        }

        adapter = new TaskPointUnitsetnumberAdapter(currentList, mActivity,this);

        mTaskPointRecycleview.setAdapter(adapter);


    }

    public void showKeybord(String curentUkid){
        currentUkid = curentUkid;
        mEasyPopupWindow = PopUpUtils.showPopUpBottom(R.layout.popupwindow_inputboard_layout, mActivity, mActivity.findViewById(android.R.id.content), true, this);
    }

    @Override
    public void getChildView(int i, View view) {

        VirtualKeyboardView keyboardView = (VirtualKeyboardView) view.findViewById(R.id.number);
        keyboardView.isShowContentBar(true,3);
        keyboardView.setOnInputDataConfirmListener(new VirtualKeyboardView.OnInputDataConfirmListener() {
            @Override
            public void InputDataConfirm(String s) {
                if(StringUtils.isNullString(s)){
                    mEasyPopupWindow.dismiss();
                    return;
                }

                try {
                    if ("00".equals(s.trim()) || "000".equals(s.trim())) {
                        adapter.setAdapterNumber("" + currentUkid , 1);
                    } else {
                        if (s.trim().startsWith("0")) {
                            if (s.trim().startsWith("00")) {
                                adapter.setAdapterNumber("" + currentUkid , Integer.parseInt(s.trim().substring(2)));
                            } else {
                                if ("0".equals(s.trim())) {
                                    adapter.setAdapterNumber("" + currentUkid, 0);
                                } else {
                                    adapter.setAdapterNumber("" + currentUkid, Integer.parseInt(s.trim().substring(1)));
                                }
                            }
                        } else {
                            adapter.setAdapterNumber("" + currentUkid, Integer.parseInt(s.trim()));
                        }
                    }

                    EventBus.getDefault().post(new JobPointSetnumberEvent());
                    mEasyPopupWindow.dismiss();
                } catch (NumberFormatException e) {
                    toast("请输入有效数量！");
                }
            }
        });

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}
